/*package com.ccl.iot;

import android.content.Context;

public class MiniRgbLight extends MiniControlerView {

	public MiniRgbLight(Context context, IOTDevice aDevice) {
		super(context, aDevice);
	}

	@Override
	public OnTouchListener getTouchListener() {
		return null;
	}

	@Override
	public OnClickListener getClickListener() {
		return null;
	}

}*/

package com.ccl.iot.controler;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;

import com.ccl.iot.privates.RgbLightIconBuilder;
import com.ccl.iot.IOTInterface.IOTStateMonitorInterface;
import com.ccl.iot.IOTNetAPIEnum;
import com.ccl.iot.device.IOTRgbLight;
import com.ccl.iot.dialogs.NoTitleDialog;
import com.ccl.iot.device.RGBBulbPickerView;
import com.ccl.iot.views.SwitchView;

public class MiniRgbLightView extends MiniControlerView implements SwitchView {
    private Paint iPaint = new Paint();

    private boolean iPressing = false;

    private boolean isToning;

    public MiniRgbLightView(Context context, IOTRgbLight aDevice) {
        super(context, aDevice);
        iPaint.setAntiAlias(true);
        iPaint.setDither(true);
        bindDevice();
    }

    @Override
    public void bindDevice() {
        super.bindDevice();

        if (getItem() != null) {
            getItem().addStateChangeListener(this);

            super.setOnTouchListener(iOnTouchListener);
            super.setOnClickListener(iOnClickListener);
        }
    }

    @Override
    public void unBindDevice() {
        if (getItem() != null) {
            getItem().removeStateChangeListener(this);
        }

        super.unBindDevice();

        super.setOnTouchListener(null);
        super.setOnClickListener(null);
    }

    @Override
    public IOTRgbLight getItem() {
        return (IOTRgbLight) iItem;
    }

    @Override
    public boolean isOn() {
        if (getItem() != null) {
            return getItem().isOn();
        }

        return false;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Rect tRect = super.getItemRect();

        if (tRect != null) {
            Bitmap tRgbLight = null;

            tRgbLight = RgbLightIconBuilder.Build(tRect.width(), tRect.height(), getItem() != null && getItem().isOnline(), isOn(), iPressing, getItem() != null ? getItem().getAuth() : IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_NONE, getItem().getSplitIconId());

            if (tRgbLight != null) {
                canvas.drawBitmap(tRgbLight, tRect.left, tRect.top, iPaint);
            } else {
                iPaint.setColor(Color.DKGRAY);

                canvas.drawRect(tRect, iPaint);
            }
        }

        super.onDraw(canvas);
    }

    private OnClickListener iExOnClickListener = null;

    @Override
    public void setOnClickListener(OnClickListener l) {
        iExOnClickListener = l;
    }

    private OnClickListener iOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (getVisibility() == VISIBLE) {
                if (isDeviceBind()) {
                    final IOTRgbLight dev = getItem();
                    checkAuth(dev);
                    if (!isToning) {
                        if (waitView != null) {
                            waitView.setVisibility(VISIBLE);
                        }
                        dev.turn(!isOn());
                    } else {
                        NoTitleDialog dialog = new NoTitleDialog(getContext());
                        dialog.setContentView(new RGBBulbPickerView(getContext(), dev));
                        dialog.show();
                    }
                    isToning = false;
                }
            }

            if (iExOnClickListener != null) {
                iExOnClickListener.onClick(v);
            }
        }
    };

    private OnTouchListener iOnTouchListener = new OnTouchListener() {
        float moveY = -1;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            IOTRgbLight dev = getItem();
            if (dev != null && dev.isOnline()) {
                if (event != null) {
                    int tAction = event.getAction();

                    if (tAction == MotionEvent.ACTION_DOWN) {
                        moveY = -1;
                        Rect tRect = getItemRect();

                        if (tRect != null) {

                            int x = (int) (((tRect.right - tRect.left) * 0.4) / 2);
                            int y = (int) (((tRect.bottom - tRect.top) * 0.4) / 2);
                            int r = (y < x ? y : x) * 2;
                            Rect rect = new Rect(tRect.left, tRect.top, tRect.left + r, tRect.top + r);

                            if (rect.contains((int) (event.getX()), (int) (event.getY()))) {
                                isToning = true;

                            } else if (event.getX() >= tRect.left && event.getY() >= tRect.top && event.getX() < tRect.right && event.getY() < tRect.bottom) {
                                iPressing = true;
                            }
                        }

                        requestDisallowInterceptTouchEvent(true);
                    } else if (tAction == MotionEvent.ACTION_UP) {
                        moveY = -1;
                        if (iPressing) {
                            iPressing = false;
                        }
                    } else if (tAction == MotionEvent.ACTION_CANCEL) {
                        moveY = -1;
                        iPressing = false;
                    } else if (tAction == MotionEvent.ACTION_MOVE) {

                        requestDisallowInterceptTouchEvent(true);
                        if (moveY == -1) {
                            moveY = event.getY();
                        }
                        if (moveY == event.getY()) {
                            requestDisallowInterceptTouchEvent(true);
                        } else {
                            if (Math.abs(moveY - event.getY()) <= 10) {
                                requestDisallowInterceptTouchEvent(true);
                            } else {
                                requestDisallowInterceptTouchEvent(false);
                            }

                        }

                    } else {
                        return false;
                    }

                    postInvalidate();

                    return true;
                }
            }

            return false;
        }
    };

    @Override
    public void onStateChanged(IOTStateMonitorInterface aSource, int aNewState) {
        super.onStateChanged(aSource, aNewState);
        this.postInvalidate();
    }
}

